package com.zlm.tcp;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Author: 郑
 * 1 连接服务器Sokcet
 * 2 发送消息
 */
public class TcpClientDemo1 {
    public static void main(String[] args) {
        Socket socket=null;
        OutputStream os=null;
        try {
            //服务器地址
            InetAddress serverIp = InetAddress.getByName("127.0.0.1");
            int prot = 9999;
            //创建socket连接
            socket = new Socket(serverIp,prot);
            //发送消息 IO流
            os = socket.getOutputStream();
            os.write("你好啊".getBytes());


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (os!=null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
